﻿using System;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;

namespace Meotom.Upload.v2.Controls
{
    public partial class FromAccount : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            var isAuthenticated = HttpContext.Current.User.Identity.IsAuthenticated;
            if (isAuthenticated)
            {
                lstUploadBy.Visible = true;
                dtPager.Visible = true;
                NotLogin.Visible = false;

                BindData();
            }
            else
            {
                NotLogin.Visible = true;
                lstUploadBy.Visible = false;
                dtPager.Visible = false;
                NotLogin.InnerHtml = "<h2 id=\"NoPhoto\">Xin vui lòng <a href='/Home/Account/Login' class='lnkLogin' style='text-decoration:underline;'>đăng nhập</a> !!!</h2>";
            }
        }

        protected void LstUploadByPreRender(object sender, EventArgs e)
        {
            if (lstUploadBy.Items.Count == 0)
            {
                dtPager.Visible = false;
            }
        }

        protected void DtPagerPreRender(object sender, EventArgs e)
        {

        }

        public string GenHtml(int uploadIdentifyId, string uploadName, DateTime uploadDate, string uploadBy)
        {
            var strOut = new StringBuilder();
            strOut.Append("<input type=\"hidden\" class=\"UploadId\" name=\"txtUploadId_").Append(uploadIdentifyId).Append(
                "\" id=\"txtUploadId_").
                Append(uploadIdentifyId).Append("\" value=\"").Append(uploadIdentifyId).Append("\" />");
            strOut.Append("<input type=\"hidden\" name=\"txtUploadName_").Append(uploadIdentifyId).Append(
                "\" id=\"txtUploadName_").
                Append(uploadIdentifyId).Append("\" value=\"").Append(uploadName).Append("\" />");
            strOut.Append("<input type=\"hidden\" name=\"txtUploadDate_").Append(uploadIdentifyId).Append(
                "\" id=\"txtUploadDate_").
                Append(uploadIdentifyId).Append("\" value=\"").Append(uploadDate).Append("\" />");
            strOut.Append("<input type=\"hidden\" name=\"txtUploadBy_").Append(uploadIdentifyId).Append(
                "\" id=\"txtUploadBy_").
                Append(uploadIdentifyId).Append("\" value=\"").Append(uploadBy).Append("\" />");

            strOut.Append("<script language=\"javascript\" type=\"text/javascript\">");
            strOut.Append("     ShowImage(").Append(uploadIdentifyId).Append(");");
            strOut.Append("</script>");
            //strOut.Append("<div id=\"UploadActive_").Append(uploadIdentifyId).Append("\" class=\"UploadActive clearfix\">");
            //strOut.Append(" <div class=\"UploadThumbImg\"><img src=\"").Append(strPath + "130x0__" + uploadName).Append("\" width=\"130\" alt=\"").Append(uploadName).Append("\" /></div>");
            //strOut.Append(" <div class=\"UploadSource clearfix\">");
            //strOut.Append("     <input type=\"checkbox\" name=\"cboCheck\" id=\"cboCheck_").Append(uploadIdentifyId).Append("\" />");
            //strOut.Append(" </div>");
            //strOut.Append("</div>");
            return strOut.ToString();
        }

        public void BindData()
        {
            var uploadBy = "";
            var userDetail = Membership.GetUser(true);
            if (userDetail != null)
                if (userDetail.ProviderUserKey != null)
                    uploadBy = userDetail.ProviderUserKey.ToString();

            using (var dc = new mtDBUploadDataContext())
            {
                lstUploadBy.DataSource = dc.GetByUser(0, uploadBy);
                lstUploadBy.DataBind();
            }

            lstUploadBy.Visible = true;
            dtPager.Visible = true;
            NotLogin.Visible = false;
        }
    }
}